﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace DBScript.Utils
{
    public class LibHelper
    {
        private static object lockobj = new object();
        /// <summary>
        /// 写入日志 by xuzhibin
        /// </summary>
        /// <param name="fileName">日志文件名，不需要带路径</param>
        /// <param name="content">日志内容，单位为行</param>
        public static void WriteLog(string fileName, string content)
        {
            //获取日志文件物理路径
            string filePath = DirectoryHelper.GetMapPath(fileName);
            string xpath = DirectoryHelper.GetFilePath(filePath);
            lock (lockobj)
            {
                if (!DirectoryHelper.Exits(xpath))
                {
                    DirectoryHelper.CreateDirectory(xpath);
                }
                try
                {
                    if (!System.IO.File.Exists(filePath))
                    {
                        StreamWriter sw = System.IO.File.CreateText(filePath);
                        sw.WriteLine("---日志开始---" + DateTime.Now.ToString());
                        sw.Flush();
                        sw.Close();
                    }
                    StreamWriter sw2 = System.IO.File.AppendText(filePath);
                    //每个日志写一行
                    sw2.WriteLine("--" + DateTime.Now.ToString() + "---" + content);
                    sw2.Flush();
                    sw2.Close();
                }
                catch (Exception ex)
                {
                    //System.Web.HttpContext.Current.Response.Write(ex.ToString());
                }
            }
        }
    }
}
